﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;

using ProjectBase.Core;

namespace ProjectBase.Mvc
{
    /// <summary>
    ///     Smart checkbox generator. Allows easy ReadOnly settings etc.
    /// </summary>
    public class CheckBox : PropertyControl<bool, CheckBox>
    {
        #region members
        #endregion members

        #region constructor
        public CheckBox() { }
        public CheckBox(bool? asReadonly = null) : base(asReadonly) { }
        #endregion constructor

        #region OnSourceProperty
        protected override void OnSourcePropertyChanged(string propertyName, bool value)
        {
            SetValue(value);
        }
        #endregion OnSourceProperty

        #region Render
        protected override void ReadControls()
        {
            Controls.Add
                (
                    new Span()
                        .SetCssClassName(GetAttribute(Attr.Class))
                        .AddControls
                        (
                            new Input(InputType.CheckBox)
                                .SetDisabled(true)
                                .SetChecked(Value)
                        )
                );
            Controls.Add
                (
                    new Input(InputType.Hidden)
                        .SetName(InputName)
                        .SetValue(Value.ToString(CultureInfo.InvariantCulture))
                );
        }

        protected override void WriteControls()
        {
            Controls.Add
                (
                    new Span()
                        .SetCssClassName(GetAttribute(Attr.Class))
                        .AddControls
                        (
                            new Input(InputType.CheckBox)
                                .SetName(InputName)
                                .SetChecked(Value)
                        )
                );
            Controls.Add
                (
                    new Input(InputType.Hidden)
                        .SetName(InputName)
                        .SetValue(Special.None)
                );
        }
        #endregion Render

        #region public Set()
        /// <summary>
        ///     Set the the current checkbox value
        ///     True will render checked="checked".
        /// </summary>
        /// <param name = "value"></param>
        /// <returns>this</returns>
        public virtual CheckBox SetValue(bool value)
        {
            Value = value;
            return this;
        }
        #endregion public Set()

        #region properties
        /// <summary>
        ///     True will render checked="checked".
        /// </summary>
        public virtual bool Value { get; set; }
        #endregion properties
    }
}